package com.gray.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gray.common.Page;
import com.gray.common.UploadImage;
import com.gray.dao.ArticleMapper;
import com.gray.dao.ArticleSortDao;
import com.gray.dto.ArticleDto;
import com.gray.pojo.Article;
import com.gray.pojo.ArticleSort;
import com.gray.pojo.User;
import com.gray.service.IArticleService;
import com.gray.util.BasePathUtil;
import com.gray.util.ImageUtil;


@Service
public class ArticleServiceImpl implements IArticleService {

	@Autowired
	private ArticleMapper articleMapper;
	
	
	@Autowired
	private ArticleSortDao articleSortDao;
	
	@Override
	public int addArticle(Article article,UploadImage uploadImage) {
		//有图片，图片后缀正确
		if(uploadImage!=null&&ImageUtil.isImageFile(uploadImage.getOriginalFilename())){
			//保存图片
			int i =  articleMapper.insertSelective(article);
			 if(i>0){

			String path = ImageUtil.imageAdd(uploadImage.getInputStream(), 
					"articleCover"+System.getProperty("file.separator")+article.getArticleId().toString(),
					uploadImage.getOriginalFilename());
			try {
				//统一格式 400*400
				ImageUtil.thumBnails(400, BasePathUtil.getImgBasePath()+path);
			} catch (IOException e) {
				throw new RuntimeException();
			}
				Article article2 = new Article();
				article2.setArticleId(article.getArticleId());
				article2.setUserId(article.getUserId());
				article2.setArticleCover(path);
				articleMapper.updateByPrimaryKeySelective(article2);
			 }
			 return i;
		}
		return articleMapper.insertSelective(article);
	
	}
	
	
	@Override
	public ArrayList<ArticleSort> getAllArticleSort(User user) {
		ArrayList list;
		
		if(user==null){
			return null;
		}else{
			list = (ArrayList) this.articleSortDao.selectArticleSort(user);
		}
		return list;
	}


	@Override
	public int delArticleSort(ArticleSort articleSort) {
		
		return this.articleSortDao.delArticleSort(articleSort);
	}


	@Override
	public int insertArticleSort(ArticleSort articleSort) {
		
		return this.articleSortDao.insertArticleSort(articleSort);
	}


	@Override
	public Map getArticleByUserAndPage(Integer userId, Integer currentPage, Integer everyPageNum) {
		// TODO Auto-generated method stub
		Integer totalNum = articleMapper.selectTotalNum(userId);
		Map map = new HashMap();
		Page page = new Page(totalNum,currentPage,everyPageNum);
		List list = articleMapper.selectByPageAndUser(userId, page.getDbFromIndex(), page.getEveryPage());
		map.put("page", page);
		map.put("article", list);
		return map;
	}


	@Override
	public int delArticleByUserAndArticleId(short articleId, Integer userId) {
		
		return articleMapper.deleteByUserAndPrimaryKey(articleId, userId);
	}


	@Override
	public Map getArticleByLikeArticleName(String articleName,Integer currentPage,Integer onePageNum) {
		Integer totalNum = articleMapper.selectAllArticleNum();//TODO Dao
		Map map = new HashMap();
		Page page = new Page(totalNum,currentPage,onePageNum);
		articleName =articleName.trim();
	List list =	articleMapper.selectByArticleNameLike(articleName,page.getDbFromIndex(),page.getEveryPage());
		map.put("article", list);
		map.put("page", page);
		return map;
	}


	@Override
	public Map getArticleByArticleClick(Integer currentPage, Integer onePageNum) {
		Integer num =  articleMapper.selectAllArticleNum();//获取数量待做 Dao 
		Page page =new Page(num,currentPage,onePageNum);
		Map map = new HashMap();
		List list = articleMapper.selectArticleByArticleClick(page.getDbFromIndex(), onePageNum);
		map.put("page", page);
		map.put("article", list);
		return map;
	}


	@Override
	public List getArticleByArticleId(short articleId) {
		List list = new ArrayList();
		 Article article=this.articleMapper.selectByPrimaryKey(articleId);
		list.add(article);
		return list;
	}


	@Override
	public Map getArticleDtoByUserAndPage(Integer userId, Integer currentPage, Integer onePageNum) {
		List list = null;
		Map map = new HashMap();
		Integer totalNum = articleMapper.selectTotalNum(1);
		Page page = new Page(totalNum,currentPage,onePageNum);
		list = this.articleMapper.selectArticleDtoByUserAndPage(userId, page.getDbFromIndex(), onePageNum);
		map.put("article", list);
		map.put("page", page);
		return map;
	}


	@Override
	public int delArticleByUserAndArticleId(Integer userId, Short articleId) {
		//TODO删除文章的同时，连评论一同删除
		return this.articleMapper.deleteByUserAndPrimaryKey(articleId, userId);
	}


	@Override
	public ArticleDto getSimpleArticleDtoByUserAndArticleId(Integer userId, Short articleId) {
		return this.articleMapper.selectArticleDtoByUserAndArticleId(userId, articleId);
	}


	@Override
	public int updateArticle(Article article,UploadImage uploadImage) {
		if(ImageUtil.isImageFile(uploadImage.getOriginalFilename())){
			String newPath = ImageUtil.imageAdd(uploadImage.getInputStream(), 
					"articleCover"+System.getProperty("file.separator")+article.getArticleId().toString(),
					uploadImage.getOriginalFilename());
			String oldPath = articleMapper.selectByPrimaryKey(article.getArticleId()).getArticleCover();
			ImageUtil.imageDel(oldPath);
			System.out.println("图片更新了..");
			article.setArticleCover(newPath);
			return articleMapper.updateByPrimaryKeySelective(article);
		}
		return this.articleMapper.updateByPrimaryKeySelective(article);
	}


	@Override
	public Map getArticleBySectionId(byte sectionId, Integer currentPage, Integer onePageNum) {
		int totalCount = articleMapper.selectArticleNumBySectionId(sectionId);
		Page page = new Page(totalCount, currentPage, onePageNum);
		Map map = new HashMap();
		List list  = articleMapper.selectArticleDtoBySection(sectionId, page.getDbFromIndex(), page.getEveryPage());
		map.put("article", list);
		map.put("page", page);
		return map;
	}


	@Override
	public List selcectCarousel() {
		List list = articleMapper.selcectCarousel();
		return list;
	}


	@Override
	public int updateArticleSort(ArticleSort articleSort) {
		
		return articleSortDao.updateArticleSort(articleSort);
	}


	
}
